package cn.yunhe.controller;

import cn.yunhe.pojo.*;
import cn.yunhe.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/userss")
public class UsersController {


    @Autowired
    private UsersService usersService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private UserInfoPService userInfoPService;

    @Autowired
    private RoleUserService roleUserService;

    @Autowired
    private RoleUserPKeyService roleUserPKeyService;

    @RequestMapping("/list")
    public String listDeptAndParent(Model model, PageBean pageBean) {
        PageBean pb = usersService.listDeptOfPage(pageBean);
        pb.setUrl("/userss/list");
        model.addAttribute("pb", pb);
        System.out.println(pb.getDatas().get(1));
        return "sysadmin/user/jUserList";
    }

    @RequestMapping("/toview")
    public String toview(String id, Model model) {
        Userp userp = usersService.findUserById(id);
        model.addAttribute("u", userp);
        System.out.println(userp);
        return "sysadmin/user/jUserView";
    }

    @RequestMapping("/toupdate")
    public String toupdate(String id, Model model) {

        List<DeptP> deptPList = deptService.selectBydept();

        model.addAttribute("ds", deptPList);

        List<UserInfoP> userInfoPList = userInfoPService.selectByUserInfo();
        //查询user_info_p表
        model.addAttribute("uis", userInfoPList);

        Userp userp = usersService.findUserById(id);
        //把修改页的数据查询，带过去 数据
        model.addAttribute("u", userp);


        UserInfoP userInfoP = userInfoPService.findUserInfoById(id);
        model.addAttribute("uf", userInfoP);


        return "sysadmin/user/jUserUpdate";

    }

    @RequestMapping("saveupdate")
    public String saveupdate(Userp userp, UserInfoP userInfoP) {

        int re = usersService.updateUserAndUserIp(userp, userInfoP);

        return "redirect:/userss/list";
    }

    @RequestMapping("/tocreate")
    public String tocreate(Model model) {

        // List<Userp> userpList=usersService.selectUserp();

        //查询部门列表
        List<DeptP> deptPList = deptService.selectBydept();

        model.addAttribute("ds", deptPList);

        List<UserInfoP> userInfoPList = userInfoPService.selectByUserInfo();
        System.out.println(userInfoPList);
        //查询user_info_p表
        model.addAttribute("uis", userInfoPList);


        return "sysadmin/user/jUserCreate";
    }

    @RequestMapping("/insert")
    public String insert(Userp userp, UserInfoP userInfoP) {

        int result = usersService.insertUserAndUserInf(userp, userInfoP);


        return "redirect:/userss/list";
    }

    @RequestMapping("torole")
    public String torole(String id, Model model) {

        model.addAttribute("id", id);
        //查询给指定用户赋予角色
        UserInfoP userInfoP = userInfoPService.findUserInfoById(id);
        model.addAttribute("userInfoP", userInfoP);
        //查询role

        List<RoleP> rolePList = roleUserService.findRoleAll();
        System.out.println(rolePList);
        model.addAttribute("rolePlist", rolePList);


        List<RoleUserPKey> roleUserPKeyList = roleUserPKeyService.listRoleUser(id);

        String[] roleidsArr = new String[roleUserPKeyList.size()];
        for (int i = 0; i < roleUserPKeyList.size(); i++) {
            RoleUserPKey roleUserPKey = roleUserPKeyList.get(i);
            roleidsArr[i] = roleUserPKey.getRoleId();  //  [1,2,3,4] 获取到的role——id
        }
        String roleids = StringUtils.join(roleidsArr, ",");  // "1,2,3,4"


        model.addAttribute("roleids", roleids);


        // 查询 当前配置的用户 已经拥有的角色


        return "sysadmin/user/jUserRole";
    }

    @RequestMapping("role")
    public String role() {

        return "redirect:/userss/list";
    }

    @RequestMapping("delete")
    @ResponseBody
    public GllsResult delete(String [] userIds){

        GllsResult result = usersService.deleteUserByUsertId(userIds);
        return result;

    }
}
